-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#639 Add Image model, update related code #646
Conversation
BackEnd/images/views.py
Outdated
context = super().get_serializer_context() | ||
image_type = self.kwargs.get("image_type") | ||
context.update({"image_type": image_type}) | ||
return context |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, there is no need to do this.
In Serializer code, you can do self.context['view'].kwargs['image_type']
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
BackEnd/images/views.py
Outdated
content_type=content_type, | ||
hash_md5=hash_md5, | ||
image_size=image_size, | ||
created_by=user, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't use unnecessary variables.
created_by=self.request.user
↑ is much cleaner than to scan the code above to find out, what exactly we put into user
variable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And the same for image_size
and image_type
(which you for some reason pass also through serializer context?..)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unnecessary variables removed
BackEnd/profiles/serializers.py
Outdated
) | ||
logo_approved_image = serializers.ImageField( | ||
source="logo_approved.image_path", required=False | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In publicly (i.e. not admin-only) avaialbe API there must be only banner
and logo
fields. Which return approved images.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
Custom ProfileImage field that contain uuid and path fields created. In publicly API only banner and logo fields are available that return approved or last uploaded (for the profile owner) images.
raise ValidationError( | ||
"Unsupported image format. Only PNG and JPEG are allowed." | ||
) | ||
with Image.open(image) as img: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Checking filename extension will be much faster.
validate_banner_size(image) | ||
else: | ||
validate_logo_size(image) | ||
return value |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
idk about if/else here. When you'll add some third data type — chances you'll remember to add it here are pretty slim (especially if it will be OK to use validate_logo_size()
).
validator_function = {
ProfileImage.BANNER: validate_banner_size,
ProfileImage.LOGO: validate_logo_size,
}[self.context["view"].kwargs["image_type"]
validator_function(value.get("image_path"))
return value
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
BackEnd/profiles/serializers.py
Outdated
return None | ||
|
||
def to_internal_value(self, data): | ||
profile_image = ProfileImage.objects.filter( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just return
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
BackEnd/profiles/serializers.py
Outdated
value.image_path.url | ||
), | ||
} | ||
return None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
None
is returned by default.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed
), | ||
"rb", | ||
self.banner_path = os.path.join( | ||
os.getcwd(), "images", "tests", "img", "img_2mb.png" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably it will be more readable:
os.path.join( os.getcwd(), "images/tests/img/img_2mb.png")
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(not sure if it will work on Windows, if no — return to the curren option).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
BackEnd/search/serializers.py
Outdated
profile_image = ProfileImage.objects.filter( | ||
uuid=data, is_deleted=False | ||
).first() | ||
return profile_image |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again, you can remove some redundant code here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
ProfileImage model has been created. Views, serializers for images and all the related code has been updated.